import { _decorator, Component, Sprite } from 'cc';
import { MapGrid } from './MapGrid';
import { buildUINodeEx, NodeEx } from './NodeEx';
import { MapResource } from './MapResource';
import { TileController } from './TileController';
import { UNIT_SIZE } from '@coldcloude/kakera';
const { ccclass } = _decorator;

@ccclass('MapTileObject')
export class MapTileObject extends Component {

    //config

    mapLayer:string;

    mapRes:MapResource;

    //init

    mapGrid:MapGrid;

    tileController: TileController;

    spriteNode:NodeEx;
    sprite:Sprite;

    _change(){
        const grid = this.mapGrid.calcGrid();
        const tile = this.mapRes.getTileObject(this.mapLayer,grid);
        if(tile){
            const [info,sprite] = tile;
            this.spriteNode.setPosition(info.positionX,info.positionY);
            this.sprite.spriteFrame = sprite;
        }
        else{
            this.spriteNode.setPosition(0,0);
            this.sprite.spriteFrame = undefined;
        }
        //DEBUG
        if(this.mapGrid.grid.x===0&&this.mapGrid.grid.y===0){
            let log = "grid layer '"+this.mapLayer+"' change to ("+grid.x+","+grid.y+")";
            log += "sprite="+(this.sprite.spriteFrame?this.sprite.spriteFrame.name:"''");
            log += " position=("+this.spriteNode.position.x+","+this.spriteNode.position.y+")";
            log += " position=("+this.node.position.x+","+this.node.position.y+")";
            let curr = this.node.parent;
            while(curr){
                log += " position=("+curr.position.x+","+curr.position.y+")";
                curr = curr.parent;
            }
            log += " world position=("+this.spriteNode.worldPosition.x+","+this.spriteNode.worldPosition.y+")";
            console.log(log);
        }
    }

    _isVisible():boolean{
        return !!this.sprite.spriteFrame;
    }

    _getSize(){
        const grid = this.mapGrid.calcGrid();
        const tile = this.mapRes.getTileObject(this.mapLayer,grid);
        if(tile){
            const [info,sprite] = tile;
            return {
                width: info.gridWidth,
                height: info.gridHeight
            }
        }
        else{
            return UNIT_SIZE;
        }
    }

    _getFlag(){
        const grid = this.mapGrid.calcGrid();
        return this.mapRes.getTag("flag",grid);
    }

    init(){
        this.mapRes.init();
        //grid
        this.mapGrid = this.getComponent(MapGrid);
        if(!this.mapGrid){
            this.mapGrid = this.addComponent(MapGrid);
        }
        //controller
        this.tileController = this.getComponent(TileController);
        if(!this.tileController){
            this.tileController = this.addComponent(TileController);
        }
        this.tileController.onChange(()=>this._change());
        this.tileController.onIsVisible(()=>this._isVisible());
        this.tileController.onGetSize(()=>this._getSize());
        this.tileController.onGetFlag(()=>this._getFlag());
        //tile
        this.spriteNode = buildUINodeEx();
        this.node.addChild(this.spriteNode);
        this.sprite = this.spriteNode.addComponent(Sprite);
        //init pos
        this._change();
    }
}
